<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
    <title>${news.title} - 教育新闻网</title>
    <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .news-container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .news-title {
            font-size: 2rem;
            font-weight: bold;
            margin-bottom: 1rem;
        }
        .news-meta {
            color: #666;
            margin-bottom: 2rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid #eee;
        }
        .news-content {
            line-height: 1.8;
            margin-bottom: 3rem;
        }
        .news-image {
            max-width: 100%;
            height: auto;
            margin: 1rem 0;
        }
        .comment-section {
            margin-top: 3rem;
            padding-top: 2rem;
            border-top: 1px solid #eee;
        }
        .comment-form {
            margin-bottom: 2rem;
        }
        .comment-list {
            margin-top: 2rem;
        }
        .comment-item {
            padding: 1rem;
            border-bottom: 1px solid #eee;
        }
        .comment-meta {
            color: #666;
            font-size: 0.9rem;
            margin-bottom: 0.5rem;
        }
        .comment-content {
            margin-bottom: 0;
        }
    </style>
</head>
<body>
    <!-- 引入头部 -->
    <jsp:include page="/user/common/header.jsp"/>
    
    <div class="news-container">
        <!-- 新闻内容 -->
        <h1 class="news-title">${news.title}</h1>
        <div class="news-meta">
            <span class="me-3">分类：${news.category}</span>
            <span class="me-3">浏览次数：${news.viewCount}</span>
            <span>发布时间：<fmt:formatDate value="${news.createTime}" pattern="yyyy-MM-dd HH:mm"/></span>
        </div>
        
        <c:if test="${not empty news.imageUrl}">
            <img src="${pageContext.request.contextPath}/${news.imageUrl}" 
                 alt="${news.title}" class="news-image">
        </c:if>
        
        <div class="news-content">
            ${news.content}
        </div>
        
        <!-- 评论区 -->
        <div class="comment-section">
            <h3>评论区</h3>
            
            <!-- 评论表单 -->
            <div class="comment-form">
                <c:choose>
                    <c:when test="${empty sessionScope.user}">
                        <div class="alert alert-warning">
                            请<a href="${pageContext.request.contextPath}/user/login.jsp" class="alert-link">登录</a>后发表评论
                        </div>
                    </c:when>
                    <c:otherwise>
                        <form id="commentForm" class="mb-4">
                            <div class="mb-3">
                                <textarea class="form-control" id="commentContent" rows="3" 
                                          placeholder="写下你的评论..."></textarea>
                            </div>
                            <button type="submit" class="btn btn-primary">发表评论</button>
                        </form>
                    </c:otherwise>
                </c:choose>
            </div>
            
            <!-- 评论列表 -->
            <div class="comment-list">
                <c:forEach items="${comments}" var="comment">
                    <div class="comment-item">
                        <div class="comment-meta">
                            <span class="fw-bold">${comment.username}</span>
                            <span class="ms-2">
                                <fmt:formatDate value="${comment.createTime}" pattern="yyyy-MM-dd HH:mm"/>
                            </span>
                        </div>
                        <p class="comment-content">${comment.content}</p>
                    </div>
                </c:forEach>
            </div>
        </div>
    </div>
    
    <!-- 引入底部 -->
    <jsp:include page="/user/common/footer.jsp"/>
    
    <script src="${pageContext.request.contextPath}/static/js/bootstrap.bundle.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#commentForm').on('submit', function(e) {
                e.preventDefault();
                
                var content = $('#commentContent').val().trim();
                if (!content) {
                    alert('请输入评论内容');
                    return;
                }
                
                $.ajax({
                    url: '${pageContext.request.contextPath}/user/news/comment',
                    type: 'POST',
                    data: {
                        newsId: ${news.newsId},
                        content: content
                    },
                    success: function(response) {
                        if (response.success) {
                            // 刷新页面显示新评论
                            location.reload();
                        } else {
                            alert(response.message || '评论失败');
                        }
                    },
                    error: function() {
                        alert('评论失败，请稍后重试');
                    }
                });
            });
        });
    </script>
</body>
</html> 